#include<cstdio>
#include<ctime>
#include<cstdlib>
#include<vector>
#include<algorithm>
using namespace std;

const int N = 600*600+17, M = 8*N;
struct Edge{int w, a, b, D;};
bool comp(Edge e1, Edge e2){return e1.w < e2.w;}
vector<Edge> edge;
int fa[N], Int[N], siz[N];
int getfa(int x){return fa[x]!=x? fa[x]=getfa(fa[x]):x;}
void merge(Edge e, bool print=false){
//	if(print)printf("%d %d %d %d\n", e.w, e.a, e.b, e.D);
	int p1 = getfa(e.a), p2 = getfa(e.b);
//	if(print)printf("%d %d\n", p1, p2);
	if(p1 == p2) return;
	fa[p1] = p2; Int[p2] = e.w; siz[p2] += siz[p1];
}
void segment(int k){
	for(int i=0; i<N; ++i) siz[i] = 1, fa[i]=i;
	sort(edge.begin(), edge.end(), comp);
//	printf("edge.size()=%d\n", edge.size());
	for(int i=0; i < edge.size(); ++i){
//		if(i >= 86460)
//			printf("%d %d %d\n", edge[i].a, edge[i].b, edge[i].w);
		int fa_a = getfa(edge[i].a);
		int fa_b = getfa(edge[i].b);
//		if(i >= 86460)
//			printf("%d(%d) %d(%d)\n", fa_a, siz[fa_a], fa_b, siz[fa_b]);
		int MInta = Int[fa_a] + k / siz[fa_a];
		int MIntb = Int[fa_b] + k / siz[fa_b];
		edge[i].D = (edge[i].w > min(MInta, MIntb));
		if(!edge[i].D) merge(edge[i]);
		// if(i >= 86460) printf("Do edge %d\n", i);
	}
	// printf("Over!\n");
}

int pix[N];
#define abs(x) ((x)>0?(x):-(x))
void calc_edge(int index1, int index2){
	Edge tmp = (Edge){0, index1, index2, 0};
	tmp.w = abs(pix[index1] - pix[index2]);
	edge.push_back(tmp);
	// printf("%d %d %d\n", pix[index1], pix[index2], abs(pix[index1] - pix[index2]));
}
void readinput(){
	freopen("mm4-P3.ppm", "r", stdin); char t;
	for(int i=1; i<=4; ++i) while((t=getchar())!='\n'); 
	for(int i=0; i<600; ++i)
		for(int j=0; j<600; ++j){
			scanf("%d%*d%*d", &pix[i*600+j]);
		}
	for(int i=1; i<600; ++i)
		for(int j=1; j<600; ++j){
			calc_edge(i*600+j, (i-1)*600+j);
			calc_edge(i*600+j, i*600+(j-1));
			calc_edge(i*600+j, (i-1)*600+(j-1));
		}
}

int randomRGB[N][3];
int get_random(){
	return rand() % 255 + 1;
}
void postprocess(){
	freopen("mm-seg.ppm", "w", stdout);
	srand(time(NULL));
	printf("P3\n# Create by SofanHe's program\n");
	printf("600 600\n255\n");
	for(int i=0; i<600; ++i)
		for(int j=0; j<600; ++j){
			int belong = getfa(i*600+j);
			if(!randomRGB[belong][0]){
				randomRGB[belong][0] = get_random();
				randomRGB[belong][1] = get_random();
				randomRGB[belong][2] = get_random();
			}
			printf("%d\n", randomRGB[belong][0]);
			printf("%d\n", randomRGB[belong][1]);
			printf("%d\n", randomRGB[belong][2]);
		}
}

int main(){
	readinput();
	segment(300);
	postprocess();
	return 0;
}
